﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using _4T.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Transactions;
using System.Globalization;

namespace _4T.DAL
{
    public class ProfileDAL
    {
        public ProfileDAL() { }

        public string InsertNewProfile(string note)
        {
            string result = string.Empty;
            ConnectionStringSettings ConnectionStringSettings =
              ConfigurationManager.ConnectionStrings["cnnString4T"];
            if (ConnectionStringSettings != null && !string.IsNullOrEmpty(ConnectionStringSettings.ConnectionString.Trim()))
            {
                string conString = ConnectionStringSettings.ConnectionString;

                SqlConnection conn = new SqlConnection(conString);
                SqlCommand cmd = new SqlCommand();
                try
                {
                    conn.Open();
                    cmd.CommandText = "ProfileInsert";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@Note", SqlDbType.NText).Value = note;
                    cmd.Connection = conn;

                    result = Convert.ToString(cmd.ExecuteScalar());
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    cmd.Dispose();
                    if (conn.State != ConnectionState.Closed)
                        conn.Close();
                }
            }

            return result;
        }

        public CMS_Profile GetProfileByProfileCode(string profileCode)
        {
            ConnectionStringSettings ConnectionStringSettings =
              ConfigurationManager.ConnectionStrings["cnnString4T"];
            if (ConnectionStringSettings != null && !string.IsNullOrEmpty(ConnectionStringSettings.ConnectionString.Trim()))
            {
                string conString = ConnectionStringSettings.ConnectionString;
                CMS_Profile profile = null;
                using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
                {
                    SqlConnection conn = new SqlConnection(conString);
                    SqlCommand cmd = new SqlCommand();
                    SqlDataReader reader = null;
                    try
                    {
                        conn.Open();
                        cmd.CommandText = "ProfileGet";
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add("@ProfileCode", SqlDbType.VarChar, 50).Value = profileCode;
                        cmd.Connection = conn;

                        reader = cmd.ExecuteReader();

                        if (reader.HasRows)
                        {
                            reader.Read();
                            profile = GetProfileFromReader(reader);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        cmd.Dispose();
                        if (conn.State != ConnectionState.Closed)
                            conn.Close();
                    }
                    scope.Complete();
                }

                return profile;
            }

            return null;
        }

        public List<CMS_Profile> GetProfileList()
        {
            ConnectionStringSettings ConnectionStringSettings =
              ConfigurationManager.ConnectionStrings["cnnString4T"];
            if (ConnectionStringSettings != null && !string.IsNullOrEmpty(ConnectionStringSettings.ConnectionString.Trim()))
            {
                string conString = ConnectionStringSettings.ConnectionString;

                SqlConnection conn = new SqlConnection(conString);
                SqlCommand cmd = new SqlCommand();
                SqlDataReader reader = null;
                try
                {
                    conn.Open();
                    cmd.CommandText = "ProfileGetListForCMS_NoPaging";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Connection = conn;
                    reader = cmd.ExecuteReader();
                    if (reader != null)
                    {
                        List<CMS_Profile> list = new List<CMS_Profile>();
                        while (reader.Read())
                        {
                            CMS_Profile item = GetProfileFromReader(reader);
                            list.Add(item);
                        }
                        return list;
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    cmd.Dispose();
                    if (conn.State != ConnectionState.Closed)
                        conn.Close();
                }
            }

            return null;
        }

        public List<CMS_Profile> GetProfileList(int page, int pageSize, string searchText)
        {
            ConnectionStringSettings ConnectionStringSettings =
              ConfigurationManager.ConnectionStrings["cnnString4T"];
            if (ConnectionStringSettings != null && !string.IsNullOrEmpty(ConnectionStringSettings.ConnectionString.Trim()))
            {
                string conString = ConnectionStringSettings.ConnectionString;

                SqlConnection conn = new SqlConnection(conString);
                SqlCommand cmd = new SqlCommand();
                SqlDataReader reader = null;
                try
                {
                    conn.Open();
                    cmd.CommandText = "ProfileGetListForCMS";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("page", SqlDbType.Int).Value = page;
                    cmd.Parameters.Add("pageSize", SqlDbType.Int).Value = pageSize;
                    cmd.Parameters.Add("searchText", SqlDbType.VarChar, 50).Value = searchText;
                    cmd.Connection = conn;
                    reader = cmd.ExecuteReader();
                    if (reader != null)
                    {
                        List<CMS_Profile> list = new List<CMS_Profile>();
                        while (reader.Read())
                        {
                            CMS_Profile item = GetProfileFromReader(reader);
                            list.Add(item);
                        }
                        return list;
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    cmd.Dispose();
                    if (conn.State != ConnectionState.Closed)
                        conn.Close();
                }
            }

            return null;
        }

        private CMS_Profile GetProfileFromReader(SqlDataReader reader)
        {
            string profileCode = string.Empty;
            if (reader.GetValue(1) != DBNull.Value)
                profileCode = reader.GetString(1);

            int stage = 0;
            if (reader.GetValue(2) != DBNull.Value)
                stage = reader.GetInt32(2);

            int status = 0;
            if (reader.GetValue(3) != DBNull.Value)
                status = reader.GetInt32(3);

            string note = string.Empty;
            if (reader.GetValue(4) != DBNull.Value)
                note = reader.GetString(4);

            string lastEditTime = string.Empty;
            if (reader.GetValue(5) != DBNull.Value)
                lastEditTime = reader.GetDateTime(5).ToString("G", CultureInfo.CreateSpecificCulture("vi-VI"));

            return new CMS_Profile(profileCode, stage, status, note, lastEditTime);
        }

        public int GetProfileTotalRecordForCMS()
        {
            int result = 0;
            ConnectionStringSettings ConnectionStringSettings =
              ConfigurationManager.ConnectionStrings["cnnString4T"];
            if (ConnectionStringSettings != null && !string.IsNullOrEmpty(ConnectionStringSettings.ConnectionString.Trim()))
            {
                string conString = ConnectionStringSettings.ConnectionString;

                SqlConnection conn = new SqlConnection(conString);
                SqlCommand cmd = new SqlCommand();
                try
                {
                    conn.Open();
                    cmd.CommandText = "ProfileGetTotalNumberForCMS";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Connection = conn;

                    result = Convert.ToInt32(cmd.ExecuteScalar());
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    cmd.Dispose();
                    if (conn.State != ConnectionState.Closed)
                        conn.Close();
                }
            }

            return result;
        }

        public CMS_VideoUploadInfo GetVideoUploadInfo(string profileInfoCode)
        {
            ConnectionStringSettings ConnectionStringSettings =
              ConfigurationManager.ConnectionStrings["cnnString4T"];
            if (ConnectionStringSettings != null && !string.IsNullOrEmpty(ConnectionStringSettings.ConnectionString.Trim()))
            {
                string conString = ConnectionStringSettings.ConnectionString;
                CMS_VideoUploadInfo video = null;
                SqlConnection conn = new SqlConnection(conString);
                SqlCommand cmd = new SqlCommand();
                SqlDataReader reader = null;
                try
                {
                    conn.Open();
                    cmd.CommandText = "GetVideoInfoByProfileInfoCode";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@ProfileInfoCode", SqlDbType.VarChar, 50).Value = profileInfoCode;
                    cmd.Connection = conn;

                    reader = cmd.ExecuteReader();

                    if (reader.HasRows)
                    {
                        reader.Read();
                        video = GetVideoUploadInfoFromReader(reader);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    cmd.Dispose();
                    if (conn.State != ConnectionState.Closed)
                        conn.Close();
                }

                return video;
            }

            return null;
        }

        private CMS_VideoUploadInfo GetVideoUploadInfoFromReader(SqlDataReader reader)
        {
            string profileInfoCode = string.Empty;
            if (reader.GetValue(0) != DBNull.Value)
                profileInfoCode = reader.GetString(0);

            string station = string.Empty;
            if (reader.GetValue(1) != DBNull.Value)
                station = reader.GetString(1);

            string uploadTime = string.Empty;
            if (reader.GetValue(2) != DBNull.Value)
                uploadTime = reader.GetDateTime(2).ToString("G", CultureInfo.CreateSpecificCulture("vi-VI"));

            return new CMS_VideoUploadInfo(profileInfoCode, station, uploadTime);
        }
    }
}